﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.IO;
using System.Xml;
using System.DirectoryServices.ActiveDirectory;
using System.Threading;
using System.Net;
using System.Text;
using System.Collections.ObjectModel;

namespace Main
{
    public class ControlCustomers
    {
        public class Model
        {
            public class Customer
            {
                private String _name;
                private String _surname;
                private String _email;
                private String _phone;
                private String _address;
                private String _company;
                private int _id;

                public Customer()
                {

                }
                public int Id
                {
                    get { return _id; }
                    set { _id = value; }
                }
                public String Name
                {
                    get { return _name; }
                    set { _name = value; }
                }
                public String Surname
                {
                    get { return _surname; }
                    set { _surname = value; }
                }
                public String Email
                {
                    get { return _email; }
                    set { _email = value; }
                }
                public String Phone
                {
                    get { return _phone; }
                    set { _phone = value; }
                }
                public String Address
                {
                    get { return _address; }
                    set { _address = value; }
                }
                public String Company
                {
                    get { return _company; }
                    set { _company = value; }
                }
            }
        }

        public class Control
        {
            ControlCommon common = new ControlCommon();
            
            /// <summary>
            /// saves new customer info on SQL Azure
            /// </summary>
            /// <param name="name"></param>
            /// <param name="surname"></param>
            /// <param name="email"></param>
            /// <param name="phone"></param>
            /// <param name="address"></param>
            /// <param name="company"></param>
            /// <returns></returns>
            public String CreateNewCustomer(string name, string surname, string email, string phone, string address, string company, string companyLogin)
            {
                try
                {
                    common.CreateLog("Starting saving of New Customer on SQL Azure for: " + name + " " + surname);
                    TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                    Customer customer = new Customer();
                    customer.name = name;
                    customer.surname = surname;
                    customer.email = email;
                    customer.phone = phone;
                    customer.address = address;
                    customer.company = company;
                    customer.CompanyLogin = companyLogin;
                    db.Customers.InsertOnSubmit(customer);
                    db.SubmitChanges();
                    db.Dispose();

                    common.CreateLog("Stopping Successful saving of New Customer on SQL Azure for: " + name + " " + surname);
                    return String.Empty;
                }
                catch (Exception e)
                {
                    common.CreateLog(e.ToString());
                    return e.ToString();
                }
            }

            /// <summary>
            ///  gets customer info from SQL Azure if any
            /// </summary>
            /// <returns></returns>
            public List<Customer> ViewCustomer(string companyLogin)
            {
                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                var query = from u in db.Customers
                            orderby u.name,u.surname ascending
                            where u.name != String.Empty && u.CompanyLogin == companyLogin
                            select u;

                return query.ToList();
            }

            /// <summary>
            /// gets customer info from SQL Azure if any as Dictionary
            /// </summary>
            /// <returns></returns>
            public Dictionary<int, String> ViewCustomerAsDict(string companyLogin)
            {
                Dictionary<int, String> list = new Dictionary<int, string>();

                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();

                var custQuery = from p in db.Customers
                                where p.CompanyLogin == companyLogin
                                orderby p.name
                                select new
                                {
                                    p.ID,
                                    p.name,
                                    p.surname
                                };

                //Convert each record into ModelCustomer object and store into list
                foreach (var record in custQuery)
                {
                    String fullName = record.name + " " + record.surname;
                    list.Add(record.ID, fullName);
                }

                return list;
            }
        }
    }
}